﻿$(document).ready(function () {
    $('.keypress-number').bind('keypress', function (e) {
        return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) ? false : true;
        //return (e.which === 8 || e.which === 0 || (e.shiftKey === false && (e.which > 47 && e.which < 58))) ? true : false;
    });

    $(".format-price").keyup(function () {
        var value = $(this).val();
        value = value.replace(/,/gi, "");
        //value = value.replace(/\./gi, "");
        //value = parseInt(value);      
        $(this).val(numberFormat(value, 0, '.', ','));
    }).blur(function () {
        var value = $(this).val();
        if (value == "" || value == 0) {
            $(this).val('0').keyup();
        }
    });

    $(".format-price").keyup();
});

function checkSubmit() {
    $(".format-price").each(function () {
        var value = $(this).val();
        value = value.replace(/,/gi, "");
        $(this).val(value);
    });
    return true;
}

function numberFormat(number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? '.' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? ',' : dec_point;
    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
    var abs = Math.abs(n).toFixed(prec);
    var _, i;
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
        _[0] = s.slice(0, i + (n < 0)) +
            _[0].slice(i).replace(/(\d{3})/g, sep + '$1');
        s = _.join(dec);
    } else {
        s = s.replace(',', dec);
    }
    return s;
}

function addSeparatorsNF(nStr, inD, outD, sep) {
    nStr += '';
    nStr = nStr.replace(/,/gi, "");
    var dpos = nStr.indexOf(inD);
    var nStrEnd = '';
    if (dpos != -1) {
        nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
        nStr = nStr.substring(0, dpos);
    }
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
        nStr = nStr.replace(rgx, '$1' + sep + '$2');
    }
    return nStr + nStrEnd;
}
//------------------------------Check Phone Input ---------------------------------------//
function checkPhoneLength(phoneNumber) {
    //check length
    if (phoneNumber.length < 10) {
       return phoneReject();
    } else {
        return true;
    }
  
    //check head number
    //Check so phone 
    //Save payment
    //Thong bao  waiting
    //Addofflinepayment
    //Update payment voi result cuar addofflinepayment
    //Thong bao ket qua

}

function phoneApprove() {
    $(".help-block p").empty();
    $(".help-block p").html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button><img src="../../Images/success.png" /><span>Số thuê bao hợp lệ</span></div>');
    return true;
}
function phoneReject() {
    $(".help-block p").empty();
    $(".help-block p").html('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button><span>Số thuê bao không hợp lệ</span></div>');
    return false;
}
